package iot.ht.controller.htuser.service;

import iot.core.data.PageData;
import iot.core.data.entity.HTUser;
import iot.core.data.mapper.MyBaseMapper;
import iot.ht.controller.htuser.data.HTUserDetailQto;
import iot.ht.controller.htuser.data.HTUserDto;
import iot.ht.controller.htuser.data.HTUserQto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface HTUserServiceMapper extends MyBaseMapper<HTUser> {

    @Select(value={
            " <script>\n"+
            "SELECT \n" +
            " count(1) \n" +
            "from htuserdept   b\n" +
            "inner join htuser a on b.HTDept_Id=#{HTDept_Id} and b.Deleted =0   and  b.HTUser_Id  =a.Id \n" +
            "inner join htdept c on b.HTDept_Id =c.Id  \n"+
            "<where>\n"+
                "<if test=\"Key!=''\"   >\n"+
                " and (a.Name like #{Key} or  a.EName like #{Key} or a.Mobile like #{Key} )\n"+
                "</if>\n"+

                "<if test=\"State!=''\"  >\n"+
                " and b.State=#{State}  \n"+
                "</if>\n"+
            "</where>\n"+
            "</script>"

    })
    int CountByQm(HTUserQto qm);


    @Select(value={" <script> \n"+
            "SELECT \n" +
            " \n" +
            "a.Id, a.Name, a.Birthday, a.Address, a.Crt_DT, a.Crt_UId, \n" +
            "a.EName, a.Edt_DT, a.Edt_UId, a.Email, a.IDCode, a.Memo, \n" +
            "a.Mobile, a.Phone, a.Sex, a.Url, a.ZW, a.HTArea_Id,\n" +
            "c.Name  as HTDept_Name,\n" +
            "c.Id  as Crt_DId,\n" +
            "b.State\n" +
            "from htuserdept   b\n" +
            "inner join htuser a on b.HTDept_Id=#{HTDept_Id} and b.Deleted =0   and  b.HTUser_Id  =a.Id \n" +
            "inner join htdept c on b.HTDept_Id =c.Id  \n"+
            "<where>\n"+
            "<if test=\"Key!=''\"   >\n"+
            " and (a.Name like #{Key} or  a.EName like #{Key} or a.Mobile like #{Key} )\n"+
            "</if>\n"+

            "<if test=\"State!=''\"  >\n"+
            " and b.State=#{State}  \n"+
            "</if>\n"+
            "</where>\n"+
            "order by #{sortField} #{sortDir}\n"+
            "limit #{PageSize} offset #{offset} \n"+
            "</script>"

    })
    List<HTUserDto> selectByQM(HTUserQto qm );
    default PageData<HTUserDto> page(HTUserQto qm){
        var r=new PageData<HTUserDto>();
        r.setTotal( CountByQm(qm));
        r.setData( selectByQM(qm));
        return r;
    }
    @Select(value={"SELECT \n" +
            " \n" +
            "a.Id, a.Name, a.Birthday, a.Address, a.Crt_DT, a.Crt_UId, \n" +
            "a.EName, a.Edt_DT, a.Edt_UId, a.Email, a.IDCode, a.Memo, \n" +
            "a.Mobile, a.Phone, a.Sex, a.Url, a.ZW, a.HTArea_Id,\n" +
            "c.Name  as HTDept_Name,\n" +
            "c.Id  as Crt_DId,\n" +
            "b.State\n" +
            "from htuserdept   b\n" +
            "inner join htuser a on b.Deleted =0 and b.HTUser_Id=#{Id} and b.HTDept_Id=#{HTDept_Id} and  b.HTUser_Id  =a.Id \n" +
            "inner join htdept c on b.HTDept_Id =c.Id  \n"})
    HTUserDto detail(Long Id,Long HTDept_Id);
}

